package com.jeepig.web.controller.evaluation;

import com.jeepig.common.annotation.Log;
import com.jeepig.common.core.controller.BaseController;
import com.jeepig.common.core.domain.AjaxResult;
import com.jeepig.common.core.page.TableDataInfo;
import com.jeepig.common.enums.BusinessType;
import com.jeepig.common.utils.SecurityUtils;
import com.jeepig.common.utils.StringUtils;
import com.jeepig.common.utils.poi.ExcelUtil;
import com.jeepig.evaluation.domain.Evaluate;
import com.jeepig.evaluation.domain.EvaluateItem;
import com.jeepig.evaluation.domain.MenberEvaluation;
import com.jeepig.evaluation.service.IEvaluateItemService;
import com.jeepig.evaluation.service.IMenberEvaluationService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 人员评价Controller
 *
 * @author guoxb
 * @date 2025-11-01
 */
@RestController
@RequestMapping("/evaluation/memberEvaluation")
public class MenberEvaluationController extends BaseController
{
    @Autowired
    private IMenberEvaluationService menberEvaluationService;

    @Autowired
    private IEvaluateItemService evaluateItemService;

    /**
     * 查询人员评价列表
     */
    @PreAuthorize("@ss.hasPermi('evaluation:memberEvaluation:list')")
    @GetMapping("/list")
    public TableDataInfo list(MenberEvaluation menberEvaluation)
    {
        startPage();
        List<MenberEvaluation> list = menberEvaluationService.selectMenberEvaluationList(menberEvaluation);
        return getDataTable(list);
    }

    /**
     * 查询人员评价列表
     *
     * @param deptId
     * @param name
     * @param status
     * @return
     */
    @GetMapping("/listAllByDeptId")
    public AjaxResult listAllByDeptId(Long deptId, String name, String status) {
        if(StringUtils.isNull(deptId) || deptId == 0L){
            deptId = SecurityUtils.getDeptId();
        }
        return AjaxResult.success(menberEvaluationService.selectMembersByDeptId(deptId, name, status));
    }

    /**
     * 查询人员评价项目列表
     * @param memberId
     * @return
     */
    @GetMapping("/listAllByMemberId")
    public AjaxResult listAllByMemberId(Long memberId, Long deptId) {
        if(StringUtils.isNull(deptId) || deptId == 0L){
            deptId = SecurityUtils.getDeptId();
        }
        return AjaxResult.success(menberEvaluationService.selectEvaluateListByMemberId(memberId, deptId));
    }
    /**
     * 查询人员评价列表
     */
    @GetMapping("/listAll")
    public AjaxResult listAll(MenberEvaluation menberEvaluation)
    {
        List<MenberEvaluation> list = menberEvaluationService.selectMenberEvaluationList(menberEvaluation);
        return success(list);
    }

    /**
     * 导出人员评价列表
     */
    @PreAuthorize("@ss.hasPermi('evaluation:memberEvaluation:export')")
    @Log(title = "人员评价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MenberEvaluation menberEvaluation)
    {
        List<MenberEvaluation> list = menberEvaluationService.selectMenberEvaluationList(menberEvaluation);
        ExcelUtil<MenberEvaluation> util = new ExcelUtil<MenberEvaluation>(MenberEvaluation.class);
        util.exportExcel(response, list, "人员评价数据");
    }

    /**
     * 获取人员评价详细信息
     */
    @PreAuthorize("@ss.hasPermi('evaluation:memberEvaluation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(menberEvaluationService.selectMenberEvaluationById(id));
    }

    /**
     * 新增人员评价
     */
    @PreAuthorize("@ss.hasPermi('evaluation:memberEvaluation:add')")
    @Log(title = "人员评价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MenberEvaluation menberEvaluation)
    {
        if(StringUtils.isNotEmpty(menberEvaluation.getDeptIds())){
            menberEvaluationService.deleteMenberEvaluationByMemberId(menberEvaluation.getMemberId(), menberEvaluation.getEvaluateId());
            for(Long deptId:menberEvaluation.getDeptIds()){
                menberEvaluation.setDeptId(deptId);
                menberEvaluationService.insertMenberEvaluation(menberEvaluation);
            }
        }
        return AjaxResult.success("保存成功！");
    }

    /**
     * 人员评价
     * @param evaluates
     * @return
     */
    @Log(title = "人员评价", businessType = BusinessType.INSERT)
    @PostMapping("/evaluate")
    public AjaxResult evaluate(@RequestBody List<Evaluate> evaluates){
        for(Evaluate evaluate: evaluates){
            MenberEvaluation menberEvaluation = menberEvaluationService.selectEvaluate(evaluate.getMemberId(), evaluate.getId(), SecurityUtils.getDeptId());
            if(StringUtils.isNotNull(menberEvaluation)){
                EvaluateItem evaluateItem = evaluateItemService.selectEvaluateItemById(String.valueOf(evaluate.getEvaluateItemId()));
                if(evaluateItem != null){
                    menberEvaluation.setScore(evaluateItem.getScore());
                    menberEvaluation.setEvaluateName(evaluateItem.getName());
                }
                menberEvaluation.setEvaluateItemId(evaluate.getEvaluateItemId());
                menberEvaluation.setRemark(evaluate.getRemark());
                menberEvaluationService.updateMenberEvaluation(menberEvaluation);
            }
        }
        return AjaxResult.success("保存成功！");
    }

    /**
     * 修改人员评价
     */
    @PreAuthorize("@ss.hasPermi('evaluation:memberEvaluation:edit')")
    @Log(title = "人员评价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MenberEvaluation menberEvaluation)
    {
        if(StringUtils.isNotEmpty(menberEvaluation.getDeptIds())){
            menberEvaluationService.deleteMenberEvaluationByMemberId(menberEvaluation.getMemberId(), menberEvaluation.getEvaluateId());
            for(Long deptId:menberEvaluation.getDeptIds()){
                menberEvaluation.setDeptId(deptId);
                menberEvaluationService.insertMenberEvaluation(menberEvaluation);
            }
        }
        return toAjax(menberEvaluationService.updateMenberEvaluation(menberEvaluation));
    }

    /**
     * 删除人员评价
     */
    @PreAuthorize("@ss.hasPermi('evaluation:memberEvaluation:remove')")
    @Log(title = "人员评价", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(menberEvaluationService.deleteMenberEvaluationByIds(ids));
    }
}
